<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
class Playlog extends Model{
	protected $auto = ['log_addtime'];
	 protected function setLogAddtimeAttr($value,$data){
		return time();
	}	
	public function user(){
		return $this->hasOne('user','user_id','log_uid');
    }
	public function vod(){
		return $this->hasOne('vod','vod_id','log_vid');
    }
	public function find(){
		if(is_login()){
            $list = db('playlog')->alias('p')->field('p.*,vod_id,vod_name,vod_letters,vod_cid,vod_pic,vod_title,vod_addtime,vod_continu,vod_gold,vod_actor,vod_year,vod_jumpurl')->join('vod v','v.vod_id=p.log_vid','LEFT')->where(['log_uid'=>is_login()])->order('log_addtime desc')->limit(10)->select();
	    }else{
		    $cookie = cookie('zanpian_playlog');
			if($cookie){
			array_multisort(array_column($cookie,'log_addtime'),SORT_DESC,$cookie);//更具播放时间排序cookie数组
			$cookie=array_slice(array_values($cookie),0,10);//重置新数组KEY并截取10
			$vid = implode(',',array_column($cookie,'log_vid'));
			$order='field(vod_id,'.$vid.')';
			$vodlist = db('vod')
			->field('vod_id,vod_name,vod_letters,vod_cid,vod_pic,vod_title,vod_addtime,vod_continu,vod_gold,vod_actor,vod_year,vod_jumpurl')
			->where('vod_id','in',$vid)
			->orderRaw($order)
			->select();
			    foreach($vodlist as $key =>$value){
				   $list[]=$value+$cookie[$key];
				}
			}
	    }
		if($list){
		    return $list;	
		}else{
		    return false;	
		}
	   
	}
	public function addajax($array){
		$data  = array();
		$data['log_vid'] = $array['log_vid'];
		$data['log_sid'] = $array['log_sid'];
		$data['log_pid'] = $array['log_pid'];
		$data['log_urlname'] = $array['log_urlname'];
		$data['log_maxnum'] = $array['log_maxnum'];
		$data['log_uid'] = $array['log_uid'];
		$data['log_addtime'] = time();
		$result=$this->save($data,['log_vid' => $data['log_vid'],'log_uid'=>$data['log_uid']]); 
		if(!$result){
			$result=$this->insert($data);	
		}
	}
	public function add($array){
		if(is_login()){
			$data  = array();
			$data['log_vid'] = $array['log_vid'];
			$data['log_sid'] = $array['log_sid'];
			$data['log_pid'] = $array['log_pid'];
			$data['log_urlname'] = $array['log_urlname'];
			$data['log_maxnum'] = $array['log_maxnum'];
			$data['log_uid'] = is_login();
			$data['log_addtime'] = time();
			$result=$this->save($data,['log_vid' => $data['log_vid'],'log_uid'=>$data['log_uid']]); 
			if(!$result){
			   $result=$this->insert($data);	
			}
	    }else{
		    $info  = array();
		    $info['id_'.$array['log_vid']]['log_vid'] = $array['log_vid'];
			$info['id_'.$array['log_vid']]['log_sid'] = $array['log_sid'];
			$info['id_'.$array['log_vid']]['log_pid'] = $array['log_pid'];
			$info['id_'.$array['log_vid']]['log_urlname'] = $array['log_urlname'];
			$info['id_'.$array['log_vid']]['log_maxnum'] = $array['log_maxnum'];
			$info['id_'.$array['log_vid']]['log_addtime'] = time();
			$cookie_old = cookie('zanpian_playlog');
			if($cookie_old){
                $datas=array_merge($cookie_old,$info);
			}else{
				$datas = $info;
			}
			$cookie=array_slice($datas,0,10);
			cookie('zanpian_playlog',$cookie, 2592000);
	    }
	}
    //数据查询	
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='log_'.$param['order'].' desc';
		}		
		if(empty($tag['order'])){
			$tag['order']='log_addtime desc';
		}	
		$where=[];
	    if(!empty($tag['uid'])) {
		    $uids = explode(',',trim($tag['uid']));
		    if(count($uids) > 1) {
			    $where['log_uid'] = array('in',$tag['uid']);
		    }else{
			    $where['log_uid'] = array('eq',$uids[0]);
		    }
	    }
	    if(!empty($tag['vid'])) {
		    $vid = explode(',',trim($tag['vid']));
		    if(count($uids) > 1) {
			    $where['log_vid'] = array('in',$tag['vid']);
		    }else{
			    $where['log_vid'] = array('eq',$vid[0]);
		    }
	    }					
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['log_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['log_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }	
        if(!empty($tag['page'])){
			$route=$GLOBALS['route'];
            $url=urldecode(url($route,config('params')));
		    $data = db('playlog')->join('vod v','v.vod_id=log_vid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = db('playlog')->join('vod v','v.vod_id=log_vid','LEFT')->where($where)->order($tag['order'])->limit($limit_str)->select();
		}	
		return $data;
    }	

}